/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/5/20 20:57
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.template.pattern.command;

import com.example.template.pattern.command.base.AbstractRemoteController;
import com.example.template.pattern.command.base.Command;
import com.example.template.pattern.command.impl.simple.NoCommand;

/**
 * <p>
 * 支持撤销的遥控器
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019-08-25 20:10
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019-08-25
 * @modify reason: {方法名}:{原因}
 * ...
 */
public class SupportUndoRemoteController extends AbstractRemoteController<Command,Command> {

    public SupportUndoRemoteController(int num) {
        onCommands = new Command[num];
        offCommands = new Command[num];
        for (int i = 0; i < num; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }

    @Override
    public void setCommand(int index,Command onCommand,Command offCommand){
        onCommands[index] = onCommand;
        offCommands[index] = offCommand;
    }



    /**
     * 遥控按钮被按下时，执行命令
     */
    @Override
    public void buttonWasPressed(int index){
        onCommands[index].execute();
        undoCommand = offCommands[index];
    }

}
